<?php
declare(strict_types=1);

namespace App\Service;

use App\Helper\Log;
use App\Helper\Util;
use Hyperf\DbConnection\Db;

/**
 * 演出服务
 */
class PlatformService 
{
    /**
     * [__construct description] 构造函数
     */
    public function __construct()
    {}
    /**
     * 平台保存
     * [platformSave description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function platformSave($params)
    {
        if(!empty($params['id'])){
            $save_data = [
                'updated_at'=>time()
            ];
            if(!empty($params['name'])){
                $save_data['name'] = $params['name'];
            }
            if(!empty($params['image'])){
                $save_data['image'] = $params['image'];
            }
            if(!empty($params['sort'])){
                $save_data['sort'] = $params['sort'];
            }
            if(isset($params['status'])){
                $save_data['status'] = $params['status'];
            }
            Db::table('platform')
                ->where(['id'=>$params['id']])
                ->update($save_data);
        }else{
            $save_data = [
                'name'=>$params['name'],
                'image'=>$params['image'],
                'sort'=>$params['sort']??0,
                'status'=>$params['status']??0,
                'created_at'=>time()
            ];
            Db::table('platform')
                ->insert($save_data);
        }
        return true;
    }
    /**
     * 根据多个ID获取名称字符串
     * [getNameStrByIds description]
     * @param  [type] $ids [description]
     * @return [type]      [description]
     */
    public function getNameStrByIds($ids)
    {
        $data = $this->getNameArrByIds($ids);
        $names_array = array_column($data, "name");
        $names = implode(",", $names_array);
        return $names;
    }
    /**
     * 根据多个ID获取名称数组
     * [getNameArrByIds description]
     * @param  [type] $ids [description]
     * @return [type]      [description]
     */
    public function getNameArrByIds($ids)
    {
        $ids_array = array_filter(explode(",", $ids));
        $return_data = [];
        foreach ($ids_array as $id) {
            $name = $this->getNameById($id);
            if(!empty($name)){
                $return_data[] = [
                    'id'=>$id,
                    'name'=>$name
                ];
            }
        }
        return $return_data;
    }
    /**
     * 根据ID获取名称
     * [getNameById description]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getNameById($id)
    {
        $name = Db::table('platform')
            ->where(['id'=>$id])
            ->value("name");
        return $name;
    }
    /**
     * 获取详情
     * [getDetail description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function getDetail($params)
    {
        $data = Db::table('platform')
            ->select(['id','name','image','openparam'])
            ->where(['id'=>$params['platform_id']])
            ->first();
        if(!empty($data)){
            $data['openparam'] = json_decode($data['openparam'], true);
        }
        return $data;
    }
}
